package com.liser.southernstar.ui.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.florent37.viewanimator.ViewAnimator;
import com.liser.southernstar.R;
import com.liser.southernstar.collection.ui.widget.ElemeAddButton;
import com.liser.southernstar.model.vo.GoodsGroupedItem;

public class ElemeAddWidget extends FrameLayout {

    private View sub;

    private TextView tv_count;

    private int count;

    private ElemeAddButton elemeAddButton;

    private boolean sub_anim, circle_anim;

    private GoodsGroupedItem goodsGroupedItem;

    private OnAddClickListener onAddClickListener;

    public ElemeAddWidget(@NonNull Context context) {
        super(context);
    }

    public ElemeAddWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.widget_eleme_add, this);
        elemeAddButton = findViewById(R.id.eleme_add_button);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddWidget);
        for (int i = 0; i < a.getIndexCount(); i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.AddWidget_circle_anim:
                    circle_anim = a.getBoolean(R.styleable.AddWidget_circle_anim, false);
                    break;
                case R.styleable.AddWidget_sub_anim:
                    sub_anim = a.getBoolean(R.styleable.AddWidget_sub_anim, false);
                    break;
            }

        }
        a.recycle();
        sub = findViewById(R.id.iv_sub);
        tv_count = findViewById(R.id.tv_count);
        elemeAddButton.setAnimListener(() -> {
            if (count == 0) {
                ViewAnimator.animate(sub)
                        .translationX(elemeAddButton.getLeft() - sub.getLeft(), 0)
                        .rotation(360)
                        .alpha(0, 255)
                        .duration(300)
                        .interpolator(new DecelerateInterpolator())
                        .andAnimate(tv_count)
                        .translationX(elemeAddButton.getLeft() - tv_count.getLeft(), 0)
                        .rotation(360)
                        .alpha(0, 255)
                        .interpolator(new DecelerateInterpolator())
                        .duration(300)
                        .start()
                ;
            }
            count++;
            tv_count.setText(count + "");
            goodsGroupedItem.setSelectCount(count);
            if (onAddClickListener != null) {
                onAddClickListener.onAddClick(elemeAddButton, goodsGroupedItem);
            }
        });
        sub.setOnClickListener(v -> {
            if (count == 0) {
                return;
            }
            if (count == 1 && sub_anim) {
                subAnim();
            }
            count--;
            tv_count.setText(count == 0 ? "1" : count + "");
            goodsGroupedItem.setSelectCount(count);
            if (onAddClickListener != null) {
                onAddClickListener.onSubClick(goodsGroupedItem);
            }
        });
    }

    private void subAnim() {
        ViewAnimator.animate(sub)
                .translationX(0, elemeAddButton.getLeft() - sub.getLeft())
                .rotation(-360)
                .alpha(255, 0)
                .duration(300)
                .interpolator(new AccelerateInterpolator())
                .andAnimate(tv_count)
                .onStop(() -> {
                    if (circle_anim) {
                        elemeAddButton.expendAnim();
                    }
                })
                .translationX(0, elemeAddButton.getLeft() - tv_count.getLeft())
                .rotation(-360)
                .alpha(255, 0)
                .interpolator(new AccelerateInterpolator())
                .duration(300)
                .start()
        ;
    }

    public void setData(OnAddClickListener onAddClickListener, GoodsGroupedItem goodsGroupedItem) {
        this.onAddClickListener = onAddClickListener;
        this.goodsGroupedItem = goodsGroupedItem;
        count = goodsGroupedItem.getSelectCount();
        if (count == 0) {
            sub.setAlpha(0);
            tv_count.setAlpha(0);
        } else {
            sub.setAlpha(1f);
            tv_count.setAlpha(1f);
            tv_count.setText(count + "");
        }
    }

    public void setState(int count) {
        elemeAddButton.setState(count > 0);
    }

    public void expendAdd(int count) {
        this.count = count;
        tv_count.setText(count == 0 ? "1" : count + "");
        if (count == 0) {
            subAnim();
        }
    }

    public interface OnAddClickListener {

        void onAddClick(View view, GoodsGroupedItem goodsGroupedItem);

        void onSubClick(GoodsGroupedItem goodsGroupedItem);
    }
}
